package Danbooru;

use strict;
use warnings;
use LWP::UserAgent;
use Mojo::DOM;

sub new {
	my $class = shift;
	my $self = { @_ };
	my $ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5';
	$self->{Url} = 'http://danbooru.donmai.us/tags' if ( not defined $self->{Url} );
	$self->{User_Agent} = LWP::UserAgent->new(agent => $ua) if ( not defined $self->{User_Agent} );
	
	return bless $self, $class;
};

sub get_categories {
	my $self = shift;
	my %result = map { $_ => undef } @_;
	my $dom = Mojo::DOM->new();
	my $ua = $self->{User_Agent};
	my $url = $self->{Url};
	
	foreach ( @_ ) {
		my $response = $ua->get($url."?utf8=%E2%9C%93&search%5Bname_matches%5D=$_&search%5Bcategory%5D=&search%5Bsort%5D=count&search%5Bhide_empty%5D=yes&commit=Search");
		$dom->parse($response->content);
		my $category = $dom->at('td[class^="category-"]');
		$result{$_} = $category->attrs('class') if ( defined $category );
	}
	
	return %result;
};

1;